package jmu.edu.yjc.controller;

import jmu.edu.yjc.domain.ResponseResult;
import jmu.edu.yjc.domain.entity.ScheduleSetting;
import jmu.edu.yjc.job.scheduled.CronTaskRegistrar;
import jmu.edu.yjc.service.ScheduleSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author yjc
 * @date 2023/4/3 23:54
 */
@RestController
@RequestMapping("/system/schedule")
public class ScheduleController {
    @Resource
    private ScheduleSettingService scheduleSettingService;

    @GetMapping("/list")
    public ResponseResult listJob(Integer pageSize , Integer pageNum , String beanName) {
        return scheduleSettingService.listJob(pageNum,pageSize,beanName);
    }
    /***
     * @description: 新增任务
     * @param: scheduleSetting
     * @return: ResponseResult
     * @author yjc
     * @date: 2023/4/4 0:25
     */
    @PostMapping("/add")
    public ResponseResult addJob(@RequestBody ScheduleSetting scheduleSetting) {
        return scheduleSettingService.addJob(scheduleSetting);
    }
    /*** 
     * @description: 更新定时任务 
     * @param: scheduleSetting 
     * @return: ResponseResult 
     * @author yjc
     * @date: 2023/4/4 0:26
     */ 
    @PostMapping("/update")
    public ResponseResult updateJob(@RequestBody ScheduleSetting scheduleSetting) {
        return scheduleSettingService.updateJob(scheduleSetting);
    }

    /***
     * @description: 删除任务
     * @param: null
     * @return: null
     * @author yjc
     * @date: 2023/4/4 0:28
     */
    @DeleteMapping("/delete/{id}")
    public ResponseResult deleteJob(@PathVariable("id") Long id) {
        return scheduleSettingService.deleteJob(id);
    }

    @PostMapping("/updateStatus")
    public ResponseResult updateJobStatus(@RequestBody ScheduleSetting scheduleSetting) {
        return scheduleSettingService.updateJobStatus(scheduleSetting);
    }

    @GetMapping("/{id}")
    public ResponseResult getJobById(@PathVariable("id") Long id) {
        return ResponseResult.okResult(scheduleSettingService.getById(id));
    }

    @PostMapping("/execute")
    public ResponseResult executeJob(@RequestBody ScheduleSetting job) {
        return scheduleSettingService.executeJob(job);
    }
}
